<?php
namespace app\backstage\logic;
use think\Db;
class AdvertUser
{
    /*获取所有数据*/
    public function getAllList($where)
    {
        $field = 'id,channel_id,advert_account,advert_media_id';
        $orderBy = ['id' => 'DESC'];
        $AdvertUser = \think\facade\App::model('AdvertUser');
        $list = $AdvertUser->getAll($where, $field, $orderBy);
        return $list;
    }

    /*列表数据获取*/
    public function getList($params)
    {
        $field = 'id,channel_id,advert_account,advert_media_id';

        if (request()->_role->alias == 'agent') {
            $channel_ids = Db::table('cms_manager_user')->where('parent_id','=',request()->_user->id)
                ->column('id');
            $params['channel_id'] = $channel_ids ? $channel_ids : [0];
        }

        $condition[] = ['channel_id', 'in', 'not_empty','channel_id'];
        $condition[] = ['advert_account', 'like', 'require','advert_account'];

        $where   = buildQueryCondition($condition, $params);

        if( isset($params['field']) ){
            $orderBy = [ $params['field'] => $params['order'] ];
        }else{
            $orderBy = ['id' => 'DESC'];
        }

        $AdvertUser = \think\facade\App::model('AdvertUser');
        $list = $AdvertUser->getList($where, $field, $params['page'], $params['limit'], $orderBy);

        if($list){

            $agent_id_gh = array_unique(array_column($list,'channel_id'));
            $agents = Db::table('cms_manager_user')->where('id','in',$agent_id_gh)->column('id,account');

            foreach($list as $k=>$v)
            {
                $list[$k]['agent_name'] = isset($agents[$v['channel_id']]) ? $agents[$v['channel_id']] : '';
                $list[$k]['media_name'] = getAdvertMediaName($v['advert_media_id']);
            }
        }

        $result['count']    = $AdvertUser->getTotalNumber($where);
        $result['list']     = $list;
        return $result;
    }

    /*数据获取*/
    public function getInfo($where)
    {
        $field = 'id,channel_id,advert_account,advert_media_id';
        $AdvertUser = \think\facade\App::model('AdvertUser');

        $info = $AdvertUser->getOne($where,$field);

        return $info;
    }

    /*数据添加*/
    public function addInfo($data)
    {
        $AdvertUser = \think\facade\App::model('AdvertUser');
        $res = $AdvertUser->insertRecord($data);

        if($res){
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

    /*数据编辑*/
    public function editInfo($where,$data)
    {
        $AdvertUser = \think\facade\App::model('AdvertUser');
        $res = $AdvertUser->updateRecord($where,$data);

        if($res){
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

    /*数据删除*/
    public function delInfo($where)
    {
        $AdvertUser = \think\facade\App::model('AdvertUser');
        $res = $AdvertUser->deleteRecord($where);

        if($res){
            return ['code'=>100,'msg'=>'操作成功'];
        }

        return ['code'=>102,'msg'=>'操作失败'];
    }

    /*数据处理*/
    public function dataProcessing($params)
    {
        $data['channel_id']         = $params['channel_id'];
        $data['advert_account']     = $params['advert_account'];
        $data['advert_media_id']    = $params['advert_media_id'];

        return ['data'=>$data];
    }

}